package com.fire.eyes.service.impl;

import java.util.Map;

import com.fire.eyes.constants.CommonConstants;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.dao.RecommendRuleMapper;
import com.fire.eyes.model.RecommendRule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.core.service.impl.TMitServiceDicBaseServiceImpls;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.BlogUtil;
import com.fire.eyes.model.RecomendRuleDTO;
import com.fire.eyes.service.IRecommendRuleService;

import net.sf.json.JSONObject;

/**
 * <p>
 * 推荐规则表 服务实现类
 * </p>
 *
 * @author zhongzifeng123
 * @since 2018-09-18
 */
@Service
public class RecommendRuleServiceImpl extends ServiceImpl<RecommendRuleMapper, RecommendRule>
		implements IRecommendRuleService {

	@Autowired
	RecommendRuleMapper recommendRuleMapper;
	@Autowired
	TMitServiceDicBaseServiceImpls tMitServiceDicBaseServiceImpls;

	@Override
	public AjaxResult getrecomend(String json) {
		Map<String, Object> param = JSON.parseObject(json, Map.class);
		String goodsid = param.get("goodsid").toString();

		if (StringUtils.isBlank(goodsid)) {
			return new AjaxResult(ErrorCodeConstants.PARAM_NULL, "商品id不能为空", "商品id不能为空");
		}
		BlogUtil.inlog(json, "", "mit/goods/getrecomend", "getrecomend", "req");
		RecommendRule recommendRule = recommendRuleMapper.selectById("RMD" + goodsid);
		RecomendRuleDTO recomendRuleDTO = new RecomendRuleDTO();
		if (recommendRule != null) {
			recomendRuleDTO = getResult(recommendRule,recomendRuleDTO);
		} else {
			//TODO 该处原为查询商品是否有推荐内容
			System.out.print("========");
		}
		AjaxResult ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", recomendRuleDTO, "获取成功");
		String jsonString = JSONObject.fromObject(ajaxResult).toString();
		BlogUtil.inlog(jsonString, "", "mit/goods/getrecomend", "getrecomend", "resp");
		return ajaxResult;
	}
	
	private RecomendRuleDTO getResult(RecommendRule recommendRule,RecomendRuleDTO recomendRuleDTO) {
		//区分内外链，如果是内链则截取后半段
		String link = recommendRule.getLink();
		String h5Address = tMitServiceDicBaseServiceImpls.getBaseByBkey(CommonConstants.PREVIEW_ADDRESS_GOODS_H5);
		String pcAddress = tMitServiceDicBaseServiceImpls.getBaseByBkey(CommonConstants.PREVIEW_ADDRESS_GOODS_PC);
		if (link.contains(h5Address)) {
			link = link.substring(h5Address.length());
		} else if (link.contains(pcAddress)) {
			link = link.substring(pcAddress.length());
		}
		recomendRuleDTO.setDescriptor(recommendRule.getDescriptor());
		recomendRuleDTO.setLink(link);
		recomendRuleDTO.setPic(recommendRule.getPic());
		recomendRuleDTO.setRmdid(recommendRule.getRmdid());
		recomendRuleDTO.setTitle(recommendRule.getTitle());
		recomendRuleDTO.setType(recommendRule.getType());
		recomendRuleDTO.setExtra1(recommendRule.getExtra1());
		recomendRuleDTO.setExtra2(recommendRule.getExtra2());
		return recomendRuleDTO;
	}
}
